package com.woniuxy.controller;

import com.woniuxy.common.DTO.ResponseResult;
import com.woniuxy.common.util.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;


/**
 * @Author: linKe
 * @Date: 2022/2/10 17:08
 * @FileName: LoginController
 * @version: 1.0
 * @Description: 使用公共类的JWT 工具类生成token
 */
@Slf4j
@RestController
public class LoginTokenController {

    @RequestMapping("/login")
    public ResponseResult<String> login(String uname, String pwd,
                                        HttpServletResponse response){
        //数据库查询验证用户名密码
        if("a".equals(uname)&&"aaa".equals(pwd)){
            // 登录成功，生产token
            String token = JwtUtil.create(uname);
            // 将token设置到响应头中
            response.setHeader("token", token);
            // 前后端分离项目，必须要暴露响应头，前端才能获取
            response.setHeader("Access-Control-Expose-Headers", "token");

            return new ResponseResult<>(200, "ok","login success");
        }
        return new ResponseResult<>(401, "not auth","login error");
    }
}
